package com.prs.crm.action.cdxh;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.cdxh.SerialNumberss;
import com.prs.crm.domain.cdxh.cd_Product;
import com.prs.crm.domain.cdxh.cd_ProductClass;
import com.prs.crm.service.cdxh.SerialNumberssService;
import com.prs.crm.service.cdxh.cd_ProductClassService;
import com.prs.crm.service.cdxh.cd_ProductService;

/**
 * 产品编辑
 * 
 * @author Administrator
 * 
 */
public class cd_ProductAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private cd_Product cdProduct;
	private cd_ProductClass cdProductClass;
	private cd_ProductService cd_ProductService;
	private cd_ProductClassService cd_ProductClassService;
	private Integer[] ids;
	private PaginationSupport pagination;
	private String isPrintName;
	private String isRushOrder;

	private SerialNumberss serialNumberss;

	private SerialNumberssService serialNumberssService;

	private List<cd_ProductClass> cdProductClassList;
	private Double cdProductPrice;

	@Action("editcd_Product")
	public String editCdProduct() {
		
//		if (cdProductClass == null && cdProduct != null) {
//			this.setCdProductClass(cdProduct.getProductClass());
//		}
		this.cdProductClassList = this.getCd_ProductClassService()
				.getCdProductClasses();
		this.setCdProductClassList(this.getCd_ProductClassService().getCdProductClasses());
		return SUCCESS;
	}
	
	
	@Action("listUpProductPrice")
	public String listUpProductPrice() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				cd_Product.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCd_ProductService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listDocCenter")
	public String listDocCenter() {

		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				cd_Product.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCd_ProductService().listByPage(criteria,
				pagination));
		return SUCCESS;

	}
	@Action("listcd_Product")
	public String listcd_Product() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				cd_Product.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCd_ProductService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listcd_ProductAlter")
	public String listcd_ProductAlter() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				cd_Product.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCd_ProductService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Clew(results = { "success", "none" })
	@Action(value = "upCdProduct", results = {
			@Result(name = "success", type = "redirect", location = "listUpProductPrice"),
			@Result(name = "error", type = "chain", location = "listUpProductPrice") })
	public String upCdProduct() {
		this.getCd_ProductService().saveOrProduct(cdProduct);
		
		this.setPrompt("产品修改提交成功");
		return SUCCESS;
	}

	@Clew(results = { "success", "none" })
	@Action(value = "saveCdProduct", results = {
			@Result(name = "success", type = "redirect", location = "listcd_Product"),
			@Result(name = "error", type = "chain", location = "editcd_Product") })
	public String saveCdProduct() {
		/**
		 * 产品编号
		 */
		String proCode="CDXHP1";
		SerialNumberss snObject=this.getSerialNumberssService().getSerialNumberss("产品");
		
		if(snObject!=null){
			proCode=snObject.getStyNumberCode();
			String  prosCode[]= (snObject.getStyNumberCode()).split("P");
			int overNums=Integer.parseInt(prosCode[1])+1;
			String putValues=prosCode[0]+"P"+overNums;
			snObject.setStyNumberCode(putValues);
			this.getSerialNumberssService().setNumCode(snObject);
		}else{		
			SerialNumberss snsObject=new SerialNumberss();
			snsObject.setStyCategory("产品");			
			snsObject.setStyNumberCode("CDXHP2");
			this.getSerialNumberssService().save(snsObject);
		}
		cdProduct.setProductcode(proCode);
		
		
		if (cdProduct.getName().trim() == null|| cdProduct.getName().equals("")) {
			this.addActionError("名称不能为空");
			return ERROR;
		}
		if (cdProduct.getType().trim() == null || cdProduct.equals("")) {
			this.addActionError("类型不能为空");
			return ERROR;
		}
		if (cdProduct.getProductColor().trim() == null
				|| cdProduct.getProductColor().equals("")) {
			this.addActionError("颜色不能为空");
			return ERROR;
		}
		if (cdProduct.getCost_price() == null || cdProduct.equals("")) {
			this.addActionError("价格不能为空");
			return ERROR;
		}
		if (cdProduct.getNorm().trim() == null
				|| cdProduct.getNorm().equals("")) {
			this.addActionError("产品规格不能为空");
			return ERROR;
		}
		if(cdProduct.getCdProductClass()==null){
			this.addActionError("产品分类不能为空");
			return ERROR;
		}
		cdProduct.setStockNums(0);
		cdProduct.setRemDate(new Date());
		this.setPrompt("产品登记提交成功");
//		this.cdProduct.setCdProductClass(this.cdProduct.getProductClass().getProductClassName());
		this.getCd_ProductService().save(cdProduct);

		return SUCCESS;
	}

	@Action("viewcd_Product")
	public String viewcd_Product() {
		
		return SUCCESS;
	}
	@Action("viewcd_Product1")
	public String viewcd_Product1() {
		
		return SUCCESS;
	}
	@Action("editDistributionDetail")
	public String editDistributionDetail() {
		
		return SUCCESS;
	}
	@Action("editDistributionUpdate")
	public String editDistributionUpdate() {
		
		return SUCCESS;
	}
	@Action("updateCdProductPrice")
	public String updateCdProductPrice() {
		
		return SUCCESS;
	}

	@Clew(results = { "success", "none" })
	@Action(value = "saveCdProductPrice", results = {
			@Result(name = "success", type = "redirect", location = "listcd_Product"),
			@Result(name = "error", type = "chain", location = "saveCdProductPrice") })
	public String saveCdProductPrice() {
		this.setPrompt("提交成功");
		return SUCCESS;
	}

	@Action(value = "removeCdProduct", results = { @Result(name = "success", type = "redirect", location = "listcd_Product") })
	public String removeCdProduct() {
		this.getCd_ProductService().batchRemove(ids);
		this.setPrompt("提交成功");
		return SUCCESS;
	}

	public cd_ProductService getCd_ProductService() {
		return cd_ProductService;
	}

	public void setCd_ProductService(cd_ProductService cdProductService) {
		cd_ProductService = cdProductService;
	}

	public cd_ProductClassService getCd_ProductClassService() {
		return cd_ProductClassService;
	}

	public void setCd_ProductClassService(
			cd_ProductClassService cdProductClassService) {
		cd_ProductClassService = cdProductClassService;
	}

	public Double getCdProductPrice() {
		return cdProductPrice;
	}

	public void setCdProductPrice(Double cdProductPrice) {
		this.cdProductPrice = cdProductPrice;
	}

	public cd_ProductClass getCdProductClass() {
		return cdProductClass;
	}

	public void setCdProductClass(cd_ProductClass cdProductClass) {
		this.cdProductClass = cdProductClass;
	}

	public String getIsPrintName() {
		return isPrintName;
	}

	public void setIsPrintName(String isPrintName) {
		this.isPrintName = isPrintName;
	}

	public String getIsRushOrder() {
		return isRushOrder;
	}

	public void setIsRushOrder(String isRushOrder) {
		this.isRushOrder = isRushOrder;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public cd_Product getCdProduct() {
		return cdProduct;
	}

	public void setCdProduct(cd_Product cdProduct) {
		this.cdProduct = cdProduct;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public List<cd_ProductClass> getCdProductClassList() {
		return cdProductClassList;
	}

	public void setCdProductClassList(List<cd_ProductClass> cdProductClassList) {
		this.cdProductClassList = cdProductClassList;
	}

	public SerialNumberss getSerialNumberss() {
		return serialNumberss;
	}

	public void setSerialNumberss(SerialNumberss serialNumberss) {
		this.serialNumberss = serialNumberss;
	}

	public SerialNumberssService getSerialNumberssService() {
		return serialNumberssService;
	}

	public void setSerialNumberssService(
			SerialNumberssService serialNumberssService) {
		this.serialNumberssService = serialNumberssService;
	}

}
